<?php
/**
 * common.php
 *
 * 前台处理
 * @author konakona
 * @date 2009-8-21
 */
class common {
	public function common(){
	}
	/**
	* 检测用户提交的数据合法后注册
	*@param $_reg 用户提交的表单数据
	**/
    public static function register($_reg) {
		$_reg = m::escapeSQL($_reg);
		if(strlen($_reg['regname'])<4 || strlen($_reg['regname'])>12){
			m::message('请输入正确的用户名','register.php');
		}
		if(strlen($_reg['regpwd'])<4 || strlen($_reg['regpwd'])>16){
			m::message('密码不对','register.php');
		}
		if(!preg_match("/^[_.0-9a-z-]+@([0-9a-z][0-9a-z-]+.)+[a-z]{2,3}$/i",$_reg['regemail'])){
			m::message('邮箱地址不对','register.php');
		}
		if (self::verifyEmail($_reg['regemail'])) { m::message('邮箱已经被注册，请重新注册','register.php'); }
		$time = time();
        $salt = substr(uniqid(rand()), -6);
        $pwd = md5(md5($_reg['regpwd']).$salt);
		$ip = m::getip();
		mysql::connect()->query("insert into ".m::$config['TABLEPREFIX']."members values('','{$_reg[regname]}','{$pwd}','{$salt}','','{$ip}','','{$_reg[regemail]}','{$_reg[regsex]}','{$_reg[regqq]}','','{$time}')");
		$u_id = mysql::connect()->insert_id();
		if($u_id!=NULL || $u_id!=0){
			//self::sendActivationMail($u_id);
		}
	}
    /**
     * 获取会员的salt值
     * @param $u_id Int
	*@return Int
     **/
	private static function getSalt($u_id){
		$salt = mysql::connect()->fetch_all("select u_salt from ".m::$config['TABLEPREFIX']."members where u_id={$u_id}");
		return $salt[0]['u_salt'];
	}
    /**
     * 验证邮箱是否已存在
     * @param $email string
	*@return bool
     **/
	private static function verifyEmail($email){
		return mysql::connect()->fetch_all("select u_id from ".m::$config['TABLEPREFIX']."members where u_email='{$email}'");
	}
	/**
	* 检测用户表单信息并登录
	*@param $_login 
	**/
	public static function login($_login){
		$_login = m::escapeSQL($_login);
		$u_id = self::getUserId($_login['loginname']);//获取id
		$email = self::getUserEmail($u_id);//获取邮箱
		if(empty($email)){
			m::message('账号不存在或密码错误','login.php');
		}
		$emailState = self::verifyActivation($email);
		if(empty($emailState)){
			//m::message('请激活账号后登陆','login.php');
		}
		if(self::verifyPassword($u_id,$_login['loginpwd'])){
			$_SESSION['k_user']=$_login['loginname'];
			$_SESSION['k_uid']=$u_id;
			m::message('登陆成功,即将转到首页','index.php');
		}else{
			m::message('密码不正确','login.php');
		}
	}
    /**
     * 用户登出
     */
    public static function logout(){
        if($_SESSION['k_user'] && $_SESSION['k_uid']){
            unset($_SESSION['k_user']);
            unset($_SESSION['k_uid']);
        }
    }
    /**
     * 首页获取点击排行
     */
    public static function clickTop(){
        return mysql::connect()->fetch_all("select * from ".m::$config['TABLEPREFIX']."movie where m_click>=1 ORDER BY m_click LIMIT 0,15");
    }
	/**
	* 检测用户的密码是否正确
	* @param $pwd string,$u_id int
	* @return bool
	**/
	private static function verifyPassword($u_id,$pwd){
		$salt = self::getSalt($u_id);
		$pwd = md5(md5($pwd).$salt);
		$datapwd = mysql::connect()->fetch_all("select u_id from ".m::$config['TABLEPREFIX']."members where u_pwd='{$pwd}'");
		if(empty($datapwd[0]['u_id'])){
			return false;
		}else{
			return true;
		}
	}
	/**
	* 检测用户邮箱是否激活
	* @param $email string 
	* @return bool
	**/
	public static function verifyActivation($email){
		$tatus = mysql::connect()->fetch_all("select u_activation from ".m::$config['TABLEPREFIX']."members where u_email='{$email}'");
		return $tatus[0]['u_activation'];
	}
	/**
	* 获取指定用户的邮箱
	* @param $u_id int 
	* @return string
	**/
	private static function getUserEmail($u_id){
		$email = mysql::connect()->fetch_all("select u_email from ".m::$config['TABLEPREFIX']."members where u_id='{$u_id}'");
		return $email[0]['u_email'];
	}
	/**
	* 获取指定用户名的id
	* @param $name string 
	* @return int
	**/
	public static function getUserId($name){
		$id = mysql::connect()->fetch_all("select u_id from ".m::$config['TABLEPREFIX']."members where u_username='{$name}'");
		return $id[0]['u_id'];
	}
	/**
	* 网页头部的选项版，如果用户登录了就显示用户的选项，没登陆就显示请登录
	* @param $name string 
	* @return 
	**/
	public static function indexOptions(){
		return isset($_SESSION['k_user'])?"欢迎回来{$_SESSION['k_user']}| <a title=\"0条站内信\" href='pm.html'><img src='http://img.xiami.com/res/img/default/letter.gif'></img></a> | <a href='?m=favorites'>我的收藏</a> <a href='profile.html'>修改资料</a> | <a href='?m=exit'>退出</a>":'您好，请<a href="register.php">注册</a> 或 <a href="login.html">登陆</a>';
	}
    /**
     * 网页的频道选项栏
     */
    public static function indexMenu(){
        $query = mysql::connect()->fetch_all("select * from ".m::$config['TABLEPREFIX']."channel ORDER BY c_id");
        return $query;
    }
    /**
     * 网页的频道选项栏
	 *param $m_id int
     */
	public static function getMovieData($m_id){
		if(!is_numeric($m_id)){
			m::message('非法参数','index.html');
		}
		$query = mysql::connect()->fetch_all("select * from ".m::$config['TABLEPREFIX']."movie where m_id={$m_id}");
        return $query;
	}
	/**
	* 首页显示友情链接
	* @return 
	**/
	public static function footerLink(){
		$link = admin::getLink();
		foreach($link as $l){
			$html .= " <a href=\"{$l[l_url]}\" target=\"_blank\" alt=\"{$l[l_alt]}\">{$l[l_name]}</a> ";
		}
		return $html;
	}
	public static function returnWebName(){
		
	}
	/**
	* 首页显示友情链接
	* @param string $type
	* @param string $keyword
	* @return 
	**/
	public static function searchMovie($type,$keyword){
		str_replace(" ",chr(32),$keyword);
		str_replace(",",chr(44),$keyword);
		str_replace(".",chr(46),$keyword);
		if($type=='片名')
			$field = "m_name";
		if($type=='演员')
			$field = "m_starring";
		m::escapeSQL($search_sql = "select * from ".m::$config['TABLEPREFIX']."movie where `{$field}` like '%{$keyword}%'");
		return mysql::connect()->fetch_all($search_sql);
	}
}









